package com.cje.singleton.demo;/**
 * @Auther: ChenJinEn
 * @Date: 2020/8/10 09:21
 * @Description: //TODO
 */

import java.io.*;

/**
 *@ClassName demo1
 *@Description TODO 懒汉单例设计模式
 *@Author checkZH
 *@Date 2020/8/10 9:21
 *@Version 1.0
 */
public class Demo1 implements Serializable,Cloneable {
    private Demo1() {
        //防止反射通过暴力手段调用构造器来创建对象
        if (demo1 !=null) {
            throw new RuntimeException();
        }
    }

    //防止别人通过反序列化来创建对象
    private Object readResolve() throws ObjectStreamException {
        return demo1;
    }
    public static Demo1 demo1;

    public static synchronized Demo1 getSingleObj(){
        if (demo1 == null) {
            demo1 = new Demo1();
        }
        return demo1;
    }

}
